import Nunjucks from "nunjucks";
import { fileURLToPath } from "url";

export const regTemplate = () => {
  return async (ctx, next) => {
    ctx.render = function (html, obj) {

      //获取views包的可被浏览器解析的绝对路径
      let viewBaseUrl = new URL("../views", import.meta.url);
      console.log(viewBaseUrl);

      let viewBasePath = fileURLToPath(viewBaseUrl);
      // console.log(viewBasePath);

      let env = Nunjucks.configure(viewBasePath);
      // console.log(env);

      ctx.response.body = env.render(html, obj);
    };
    await next();
  };
};
